﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Dragon.Prototype1.Service;

namespace Dragon.Prototype1.Infrastructure.CodeGenerator
{
    public class CodeBase : ICode
    {
        public CodeBase(string codeName)
        {
            this.codeName = codeName;
        }
        private string codeName = null;
        public virtual string CodeName
        {
            get { return this.codeName; }
        }

        public virtual string Format
        {
            get
            {
                return CodeName + "{0:000}";
            }
        }
        public virtual bool GetNextCode()
        {
            CodeService cs = new CodeService();
            return cs.GetNextCode(this.CodeName,1,new Func<int,string>(CalculateNextCodeInternal));
        }
        public virtual string CalculateNextCode(int currentNo)
        {
            return string.Format(this.Format, currentNo);
        }
        internal string CalculateNextCodeInternal(int currentNo)
        {
            lastcode = CalculateNextCode(currentNo);
            return lastcode;
        }
        private string lastcode = null;
        public virtual string LastCode
        {
            get
            {
                if (lastcode == null)
                    throw new InvalidOperationException("Please call GetNewCode() first");
                return lastcode;
            }
        }
    }
}
